package com.transsion.clouddemo.core.Custom

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri


class CopyLinkTextHelper private constructor(context: Context) {

    companion object {
        private var instance: CopyLinkTextHelper? = null
        private lateinit var manager: ClipboardManager

        @Synchronized
        fun getInstance(context: Context): CopyLinkTextHelper? {
            if (instance == null) {
                instance = CopyLinkTextHelper(context)
            }
            return instance
        }
    }
    init {
        //获取剪贴板管理器：
        manager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    }

    /**
     * 复制文字到剪切板
     * @param text
     */
    fun copyText(text: String?) {

        val mClipData = ClipData.newPlainText("Label", text)
        manager.setPrimaryClip(mClipData)
    }

    /**
     * 复制链接url到剪切板
     * @param url
     */
    fun copyUrl(url: String?) {

        val mClipData = ClipData.newRawUri("Label", Uri.parse(url))
        manager.setPrimaryClip(mClipData)
    }

    /**
     * 复制Intent到剪切板
     * @param intent
     */
    fun copyIntent(intent: Intent?) {

        val mClipData = ClipData.newIntent("Label", intent)
        manager.setPrimaryClip(mClipData)
    }


    /**
     *
     * 从剪贴板中获取数据,如text文字，链接等，
     * @return
     */
    fun getCopyString(): String {

        val primaryClip = manager.primaryClip
        if (primaryClip?.itemCount!! > 0) {
            return primaryClip.getItemAt(0).text.toString()
        }
        return ""
    }

}